﻿// See https://aka.ms/new-console-template for more information
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;

ConnectionFactory factory = new ConnectionFactory()
{
    UserName = "admin",
    Password = "as103208900",
    HostName = "110.41.160.73",
    VirtualHost = "my_vhost"
};

using (var connection = factory.CreateConnection())
{
    //创建通道
    using (var channel = connection.CreateModel())
    {
        //时间基本消费者
        EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

        //接收到消息事件
        consumer.Received += (ch, ea) =>
        {
            var message = Encoding.UTF8.GetString(ea.Body.ToArray());
            Console.WriteLine("收到消息：" + message);

            //确认该消息已被消费
            channel.BasicAck(ea.DeliveryTag, false);
        };

        //启动消费者，设置为手动应答消息
        channel.BasicConsume("hello", false, consumer);
        Console.WriteLine("消费者2已启动");
        Console.ReadKey();
    }
}